﻿using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;

namespace Inovout.Serialization
{
    public class XmlSerializer : ISerializer
    {
        public Stream Serialize(object obj)
        {
            if (typeof(IDictionary<,>).IsAssignableFrom(obj.GetType()))
            {
            }
            var serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
            MemoryStream stream = new MemoryStream();

            serializer.Serialize(stream, obj);
            stream.Position = 0;
            return stream;

        }
        public T Deserialize<T>(string xml)
        {
            using (StringReader stringReader = new StringReader(xml))
            {
                return Deserialize<T>(stringReader);
            }
        }
        public T Deserialize<T>(TextReader textReader)
        {
            var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
            return (T)serializer.Deserialize(textReader);
        }
        public T Deserialize<T>(Stream stream)
        {
            var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
            return (T)serializer.Deserialize(stream);
        }
    }
}
